package com.org.gameloft.GLIDER.plugins.server;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Hashtable;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: Fili
 * Date: Jul 31, 2007
 * Time: 11:09:47 PM
 */
public class PluginManager
{
    private Hashtable plugins;


    public PluginManager()
    {
        plugins = new Hashtable(10);
        refreshList();
    }

    public void refreshList()
    {
        File dir = new File("plugins");

        FilenameFilter filter = new FilenameFilter()
        {
            public boolean accept(File dir, String name)
            {
                return name.toLowerCase().endsWith(".jar");
            }
        };

        String[] items = dir.list(filter);

        Hashtable newPlugins = new Hashtable(items.length);

        for (int i = 0; i < items.length; i++)
        {
            String name = items[i].toLowerCase();
            PluginInfo pi = (PluginInfo) plugins.get(name);
            if (pi == null)
                pi = PluginInfo.getInfo("plugins/"+name);

            if (pi != null)
                newPlugins.put(name, pi);
        }

        plugins = newPlugins;
    }

    public int getPluginCount()
    {
        return plugins.size();
    }

    public void loadPlugins()
    {
        while (plugins.elements().hasMoreElements())
        {
            PluginInfo pi = (PluginInfo) plugins.elements().nextElement();
            pi.load();
        }
    }

    public void unloadPlugins()
    {

    }

    public void reloadPlugins()
    {

    }

    public void loadPlugin(int index)
    {

    }

    public void unloadPlugin(int index)
    {

    }

    public void reloadPlugin(int index)
    {

    }
}
